﻿using Compass.Wpf.ApiServices;

namespace Compass.Wpf.ViewModels;
public class MainViewModel : NavigationViewModel, IConfigureService
{
    #region ctor-主界面
    private readonly IPreferenceService _preferenceService;
    public MainViewModel(IContainerProvider provider):base(provider)
    {
        _preferenceService = provider.Resolve<IPreferenceService>();
        
        GoBackCommand = new DelegateCommand(() =>
        {
            if (Journal is { CanGoBack: true }) Journal.GoBack();
        });
        GoForwardCommand = new DelegateCommand(() =>
        {
            if (Journal is { CanGoForward: true }) Journal.GoForward();
        });
        HomeCommand = new DelegateCommand(() =>
        {
            RegionManager.Regions[PrismManager.MainViewRegionName].RequestNavigate("IndexView", back => { Journal = back.Context.NavigationService.Journal; });
        });
        LogoutCommand =new DelegateCommand(() => { App.Logout(provider, RegionManager); });//注销登录
        CurrentMenuBar=new MenuBar();
    }

    public DelegateCommand GoBackCommand { get; }
    public DelegateCommand GoForwardCommand { get; }
    public DelegateCommand HomeCommand { get; }
    //退出登录
    public DelegateCommand LogoutCommand { get; }

    #endregion

    #region 属性
    private MenuBar currentMenuBar;
    public MenuBar CurrentMenuBar
    {
        get => currentMenuBar;
        set
        {
            SetProperty(ref currentMenuBar, value);
            Navigate();
        }
    }

    /// <summary>
    /// 菜单集合
    /// </summary>
    public ObservableRangeCollection<MenuBar> MenuBars { get; } = new();
    //用于绑定菜单栏显示用户名
    private string? userName;
    public string? UserName
    {
        get => userName;
        set { userName = value; RaisePropertyChanged(); }
    }
    #endregion

    /// <summary>
    /// 导航上下页面
    /// </summary>
    /// <param name="obj"></param>
    private void Navigate()
    {
        UserName=_preferenceService.Get(Consts.UserName);
        if (CurrentMenuBar==null||string.IsNullOrWhiteSpace(CurrentMenuBar.NameSpace)) return;
        RegionManager.Regions[PrismManager.MainViewRegionName].RequestNavigate(CurrentMenuBar.NameSpace, back =>
        {
            Journal = back.Context.NavigationService.Journal;
        });
    }

    /// <summary>
    /// 初始化菜单集合
    /// </summary>
    void CreateMenuBar()
    {
        MenuBars.Add(new MenuBar { Icon = "Home", Title = "首页", NameSpace = "IndexView" });
        MenuBars.Add(new MenuBar { Icon = "Notebook", Title = "待办", NameSpace = "TodoView" });
        MenuBars.Add(new MenuBar { Icon = "NotebookPlus", Title = "备忘", NameSpace = "MemoView" });
        MenuBars.Add(new MenuBar { Icon = "Cog", Title = "设置", NameSpace = "SettingsView" });
        CurrentMenuBar = MenuBars[0];
    }

    /// <summary>
    /// 初始化配置默认首页
    /// </summary>
    public void Configure()
    {
        CreateMenuBar(); //模拟生成菜单数据，初始化菜单集合
        UserName=_preferenceService.Get(Consts.UserName);//初始化用户名
        RegionManager.Regions[PrismManager.MainViewRegionName].RequestNavigate("IndexView", back => { Journal = back.Context.NavigationService.Journal; });
    }
}
